Telegram Group & Telegram Channel
merge
Python, List
Merges two or more lists into a list of lists, combining elements from each of the input lists based on their positions.

.๐Ÿ‘‰๐Ÿป Use max() combined with a list comprehension to get the length of the longest list in the arguments.

.๐Ÿ‘‰๐Ÿป Use range() in combination with the max_length variable to loop as many times as there are elements in the longest list.

.๐Ÿ‘‰๐ŸปIf a list is shorter than max_length, use fill_value for the remaining items (defaults to None).

.๐Ÿ‘‰๐Ÿปzip() and itertools.zip_longest() provide similar functionality to this snippet.


Code:
def merge(*args, fill_value = None):
max_length = max([len(lst) for lst in args])
result = []
for i in range(max_length):
result.append([
args[k][i] if i < len(args[k]) else fill_value for k in range(len(args))
])
return result

Example:

merge(['a', 'b'], [1, 2], [True, False])

Output: [['a', 1, True], ['b', 2, False]]

Share and Support
@Python_Codes



tg-me.com/python_codes/161
Create:
Last Update:

merge
Python, List

Merges two or more lists into a list of lists, combining elements from each of the input lists based on their positions.

.๐Ÿ‘‰๐Ÿป Use max() combined with a list comprehension to get the length of the longest list in the arguments.

.๐Ÿ‘‰๐Ÿป Use range() in combination with the max_length variable to loop as many times as there are elements in the longest list.

.๐Ÿ‘‰๐ŸปIf a list is shorter than max_length, use fill_value for the remaining items (defaults to None).

.๐Ÿ‘‰๐Ÿปzip() and itertools.zip_longest() provide similar functionality to this snippet.


Code:
def merge(*args, fill_value = None):
max_length = max([len(lst) for lst in args])
result = []
for i in range(max_length):
result.append([
args[k][i] if i < len(args[k]) else fill_value for k in range(len(args))
])
return result

Example:

merge(['a', 'b'], [1, 2], [True, False])

Output: [['a', 1, True], ['b', 2, False]]

Share and Support
@Python_Codes

BY Python Codes


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/python_codes/161

View MORE
Open in Telegram


Python Codes Telegram | DID YOU KNOW?

Date: |

Telegram Auto-Delete Messages in Any Chat

Some messages arenโ€™t supposed to last forever. There are some Telegram groups and conversations where itโ€™s best if messages are automatically deleted in a day or a week. Hereโ€™s how to auto-delete messages in any Telegram chat. You can enable the auto-delete feature on a per-chat basis. It works for both one-on-one conversations and group chats. Previously, you needed to use the Secret Chat feature to automatically delete messages after a set time. At the time of writing, you can choose to automatically delete messages after a day or a week. Telegram starts the timer once they are sent, not after they are read. This wonโ€™t affect the messages that were sent before enabling the feature.

The Singapore stock market has alternated between positive and negative finishes through the last five trading days since the end of the two-day winning streak in which it had added more than a dozen points or 0.4 percent. The Straits Times Index now sits just above the 3,060-point plateau and it's likely to see a narrow trading range on Monday.

Python Codes from kr


Telegram Python Codes
FROM USA